<template>
  <div class="page">
    <p class="attributes">补充资料</p>

    <div class="basicinfo">
      <el-form
        label-position="left"
        label-width="100px"
        :model="basicinfo"
        ref="basicinfo"
      >
        <el-form-item label="公司简称">
          <el-input v-model="basicinfo.company"></el-input>
        </el-form-item>
        <el-form-item label="供应区域">
          <el-select
            v-model="basicinfo.shen"
            placeholder="选择省"
            @change="qinqiu_shi({ zhi: 1, id: basicinfo.shen })"
          >
            <el-option
              v-for="item in diqu.shen"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select
            v-model="basicinfo.shi"
            placeholder="选择市"
            @change="qinqiu_qu({ zhi: 2, id: basicinfo.shi })"
          >
            <el-option
              v-for="item in diqu.shi"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select v-model="basicinfo.qu" multiple placeholder="选择区">
            <el-option
              v-for="item in diqu.qu"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="basicinfo.address"></el-input>
        </el-form-item>
        <el-form-item label="结算周期">
          <el-select v-model="basicinfo.cycle" placeholder="请选择">
            <el-option label="周" value="0"></el-option>
            <el-option label="月" value="1"></el-option>
            <el-option label="季" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="msg">
        <div class="font">营业执照</div>
        <el-upload
          class="avatar-uploader"
          :action="dizhi"
          :headers="headers"
          :show-file-list="false"
          accept="image/png,image/jpg,image/jpeg"
          :on-success="licensesuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="licenseurl" :src="licenseurl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <div class="msg">
        <div class="font">食品流通许可证</div>
        <el-upload
          class="avatar-uploader"
          :action="dizhi"
          :headers="headers"
          :show-file-list="false"
          accept="image/png,image/jpg,image/jpeg"
          :on-success="foodsuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="foodurl" :src="foodurl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <div class="msg">
        <div class="font">法人代表身份证复印件正面</div>
        <el-upload
          class="avatar-uploader"
          :action="dizhi"
          :headers="headers"
          :show-file-list="false"
          accept="image/png,image/jpg,image/jpeg"
          :on-success="idfrontsuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="idfronturl" :src="idfronturl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <div class="msg">
        <div class="font">法人代表身份证复印件反面</div>
        <el-upload
          class="avatar-uploader"
          :action="dizhi"
          :headers="headers"
          :show-file-list="false"
          accept="image/png,image/jpg,image/jpeg"
          :on-success="idreversesuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="idreverseurl" :src="idreverseurl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
    </div>

    <div class="btn">
      <el-button type="info" class="cancel">取消</el-button>
      <el-button type="primary" @click="confirm()">确认</el-button>
    </div>
  </div>
</template>

<script>
import { replenishData } from "@/api/partner.js";
import { diqu } from "@/api/gonyin.js";
import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
      basicinfo: {
        company: "",
        address: "",
        cycle: "",
        shen: "",
        shi: "",
        qu: ""
      },
      dizhi: "http://sun.xinlinju.net/oss/upload",
      // 图片地址
      licenseurl: "",
      foodurl: "",
      idfronturl: "",
      idreverseurl: "",
      // token请求头
      headers: { token: getToken() },
      diqu: { shen: [], shi: [], qu: [] }
    };
  },
  mounted() {
    this.region();
  },
  methods: {
    // 营业执照
    licensesuccess(res, file) {
      console.log(res, file);
      this.licenseurl = res.extParams.path;
    },
    // 食品流通证
    foodsuccess(res, file) {
      console.log(res, file);
      this.foodurl = res.extParams.path;
    },
    // 身份证正面
    idfrontsuccess(res, file) {
      console.log(res, file);
      this.idfronturl = res.extParams.path;
    },
    // 身份证反面
    idreversesuccess(res, file) {
      console.log(res, file);
      this.idreverseurl = res.extParams.path;
    },
    beforeAvatarUpload(file) {
      console.log(file, "图片上传");
      const isJPG = file.type == "image/jpeg" || file.type == "image/png";
      const isLt2M = file.size / 1024 / 1024 < 4;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 或者PNG格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 4MB!");
      }
      return isJPG && isLt2M;
    },
    // 地区查询
    region() {
      let data = { areaParentId: 1 };
      diqu(data).then(res => {
        console.log(res);
        this.diqu.shen = res.rows;
      });
    },
    qinqiu_shi(obj) {
      let data = { areaParentId: obj.id };
      diqu(data).then(res => {
        console.log(res);
        this.diqu.shi = res.rows;
        this.basicinfo.shi = "";
        this.basicinfo.qu = "";
      });
    },
    qinqiu_qu(obj) {
      let data = { areaParentId: obj.id };
      diqu(data).then(res => {
        this.diqu.qu = res.rows;
        this.basicinfo.qu = "";
      });
    },
    // 确认提交按钮
    confirm() {
      if (this.basicinfo.company === "") {
        this.$message.error("请填写公司简称");
      } else if (this.basicinfo.address === "") {
        this.$message.error("请填写公司地址");
      } else if (this.basicinfo.shen == "") {
        this.$message.error("请选择供应区域省");
      } else if (this.basicinfo.shi == "") {
        this.$message.error("请选择供应区域市");
      } else if (this.basicinfo.qu == "") {
        this.$message.error("请选择供应区域区");
      } else if (this.basicinfo.cycle === "") {
        this.$message.error("请选择结算周期");
      } else if (this.licenseurl === "") {
        this.$message.error("请上传营业执照");
      } else if (this.foodurl === "") {
        this.$message.error("请上传食品流通许可证");
      } else if (this.idfronturl === "") {
        this.$message.error("请上传法人身份证正面");
      } else if (this.idreverseurl === "") {
        this.$message.error("请上传法人身份证反面");
      } else {
        let data = {
          supplierId: Number(this.$route.query.supplierId),
          company: this.basicinfo.company,
          supplyArea: [
            this.basicinfo.shen,
            this.basicinfo.shi,
            ...this.basicinfo.qu
          ],
          address: this.basicinfo.address,
          cycle: this.basicinfo.cycle,
          runLicense: this.licenseurl,
          foodLicense: this.foodurl,
          representativeLicense: this.idfronturl,
          representativeLicenseReverse: this.idreverseurl
        };
        replenishData(data).then(res => {
          console.log(res);
          if (res.code === 20000) {
            this.$message({
              message: "操作成功",
              type: "success"
            });
            this.$router.push({
              path: "supplierList"
            });
          } else {
            this.$message.error(res.msg);
          }
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.page {
  padding: 20px 20px;
  .attributes {
    font-size: 22px;
    margin-bottom: 60px;
  }
  .basicinfo {
    max-width: 850px;
    margin-left: 66px;
    .msg {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 10px;
      .font {
        font-size: 17px;
        color: #606266;
      }
    }
  }
  .btn {
    margin-top: 50px;
    text-align: center;
    .cancel {
      margin-right: 16px;
    }
  }
  .avatar-uploader {
    border: 1px dashed #777676;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
